package com.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.beans.AdminInfo;
import com.beans.Goodsinfo;
import com.common.Common;
import com.dao.GoodsDao;
import com.utils.PageInfo;
import com.utils.PageUtil;

/**
 * 商品管理
 */
@MultipartConfig // 文件上传
@WebServlet("/GoodsServlet.do")
public class GoodsServlet extends HttpServlet {
	private GoodsDao _goodsDao = new GoodsDao();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=UTF-8");
		String flag = request.getParameter("flag");
		if ("add".equals(flag)) {
			add(request, response);
		} else if ("manage".equals(flag)) {
			manage(request, response);
		} else if ("showPicture".equals(flag)) {
			showPicture(request, response);
		} else if ("delete".equals(flag)) {
			delete(request, response);
		} else if ("deleteAll".equals(flag)) {
			deleteAll(request, response);
		} else if ("edit".equals(flag)) {
			edit(request, response);
		}else if ("update".equals(flag)) {
			update(request, response);
		}
	}

	// 添加商品
	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String goodsName = request.getParameter("goodsName");
		String unit = request.getParameter("unit");
		String producter = request.getParameter("producter");
		String des = request.getParameter("des");
		float price = Float.parseFloat(request.getParameter("price"));
		int bigCateId = Integer.parseInt(request.getParameter("bigCateId"));
		int smallCateId = Integer.parseInt(request.getParameter("smallCateId"));
		// 把图片对应的输入流,转成字节数组
		Part part = request.getPart("pictureData");
		InputStream in = part.getInputStream();
		byte[] pictureData = new byte[in.available()];
		in.read(pictureData);

		Goodsinfo goods = new Goodsinfo();
		goods.setPictureData(pictureData);
		goods.setGoodsName(goodsName);
		goods.setBigCateId(bigCateId);
		goods.setSmallCateId(smallCateId);
		goods.setUnit(unit);
		goods.setPrice(price);
		goods.setProducter(producter);
		goods.setDes(des);

		int goodsId = _goodsDao.addGoods(goods);
		if(goodsId>0) {
			request.setAttribute("msg", "商品添加成功");
			request.getRequestDispatcher("/goods/goods_add.jsp").forward(request, response);
		}
		
	}

	/*
	 * 商品管理
	 */
	private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String smallCateId = request.getParameter("smallCateId");
		String goodsName = request.getParameter("goodsName");

		int pageSize = Common.PAGE_SIZE;
		int rowCount = _goodsDao.getGoodsCount(smallCateId, goodsName);
		int pageIndex = 1;
		String pageIndexStr = request.getParameter("pageIndex");
		if (pageIndexStr != null) {
			pageIndex = Integer.parseInt(pageIndexStr);
		}
		PageInfo page = PageUtil.getPageInfo(pageSize, rowCount, pageIndex);
		List<Goodsinfo> goodsList = _goodsDao.getGoodsList( smallCateId, goodsName, page);
		request.setAttribute("page", page);
		request.setAttribute("goodsList", goodsList);
		request.getRequestDispatcher("/goods/goods_manage.jsp").forward(request, response);

	}

	/*
	 * 显示图片
	 */
	private void showPicture(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int goodsId = Integer.parseInt(request.getParameter("goodsId"));
		Goodsinfo goods = _goodsDao.getGoods(goodsId);
		response.setContentType("image/jpg");
		ServletOutputStream outstream = response.getOutputStream();
		outstream.write(goods.getPictureData());
		outstream.flush();
	}

	/*
	 * 删除商品
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		int idnumber = Integer.parseInt(id);
		int rest = _goodsDao.delGoods(idnumber);
		if (rest == 1) {
			manage(request, response);
		}
	}

	/*
	 * 批量删除商品
	 */
	private void deleteAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String[] idList = request.getParameterValues("ck_id");
		_goodsDao.delAllGoods(idList);
		manage(request, response);

	}

	/*
	 * 修改商品页
	 */
	private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int goodsId = Integer.parseInt(request.getParameter("id"));
		Goodsinfo goods = _goodsDao.getGoods(goodsId);
		request.setAttribute("goods", goods);
		request.getRequestDispatcher("/goods/goods_edit.jsp").forward(request, response);

	}
	
	/*
	 * 修改商品
	 */
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int goodsId = Integer.parseInt(request.getParameter("id"));
		String goodsName = request.getParameter("goodsName");
		String unit = request.getParameter("unit");
		String producter = request.getParameter("producter");
		String des = request.getParameter("des");
		float price = Float.parseFloat(request.getParameter("price"));
		int bigCateId = Integer.parseInt(request.getParameter("bigCateId"));
		int smallCateId = Integer.parseInt(request.getParameter("smallCateId"));
		
		// 把图片对应的输入流,转成字节数组
		Part part = request.getPart("pictureData");
		InputStream in = part.getInputStream();
		byte[] pictureData;
		if(in.available()>0) {
			pictureData = new byte[in.available()];
			in.read(pictureData);
		}else {
			Goodsinfo goodsinfo=_goodsDao.getGoods(goodsId);
			pictureData = goodsinfo.getPictureData();
		}
		Goodsinfo goods = new Goodsinfo();
		goods.setId(goodsId);
		goods.setPictureData(pictureData);
		goods.setGoodsName(goodsName);
		goods.setBigCateId(bigCateId);
		goods.setSmallCateId(smallCateId);
		goods.setUnit(unit);
		goods.setPrice(price);
		goods.setProducter(producter);
		goods.setDes(des);
		int resut=_goodsDao.updateGoods(goods);
		if (resut==1) {
			manage(request, response);
		}
		

	}
}
